package com.ruoyi.ly.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ly.domain.Project;
import com.ruoyi.ly.domain.dto.PageProjectDTO;
import com.ruoyi.ly.domain.dto.ProjectAddDTO;
import com.ruoyi.ly.domain.dto.ProjectUpdateDTO;
import com.ruoyi.ly.domain.vo.*;
import com.ruoyi.ly.mapper.ProjectMapper;
import com.ruoyi.ly.mapper.ServeExaminePlanMapper;
import com.ruoyi.ly.mapper.SpaceBanMapper;
import com.ruoyi.ly.mapper.WorkSpaceMapper;
import com.ruoyi.ly.service.ProjectService;
import com.ruoyi.ly.utils.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;



    //分页查询
    @Override
    public PageResult pageQuery(PageProjectDTO pageProjectDto) {

        PageHelper.startPage(pageProjectDto.getPage(),pageProjectDto.getPageSize());
        Page<PageProjectVO> page = projectMapper.pageQuery(pageProjectDto);
        long total = page.getTotal();
        List<PageProjectVO> records = page.getResult();
        return new PageResult(total,records);
    }

    //添加项目
    @Override
    public void insert(ProjectAddDTO projectAddDto) {

        //TODO 对项目名称进行判断重名不可以新增
        Integer i = projectMapper.selectByRepeatName(projectAddDto.getProjectName());
        if (i != 0){
            throw new ServiceException("项目名称已存在");
        }
        //获取当前用户登录的用户id
        Long server_id = SecurityUtils.getUserId();
        //获取当前用户登录的用户名
        String server_name = SecurityUtils.getUsername();
        Project project = new Project();
        BeanUtils.copyProperties(projectAddDto,project);
        project.setServerId(server_id);
        project.setServerName(server_name);
        project.setCtime(String.valueOf(LocalDateTime.now()));

        projectMapper.insert(project);

    }

    //删除项目
    @Override
    public void delete(Integer id) {
        projectMapper.delete(id);
    }

    //修改项目
    @Override
    public void update(ProjectUpdateDTO projectUpdateDto) {

        //TODO 对项目名称进行判断重名不可以修改
        Integer i = projectMapper.selectByRepeatName(projectUpdateDto.getProjectName());
        //查询修改前的项目名称
        String oldProjectName = projectMapper.selectById(projectUpdateDto.getId()).getProjectName();
        //判断修改前项目名称和修改后项目名称是否一致
        boolean equals = oldProjectName.equals(projectUpdateDto.getProjectName());

        //修改时不能与其他项目名称重复
        if (equals) {
            Project project = new Project();
            BeanUtils.copyProperties(projectUpdateDto,project);
            projectMapper.update(project);
        }
        else{
            if (i != 0) {
                throw new ServiceException("项目名称已存在");
            }
            Project project = new Project();
            BeanUtils.copyProperties(projectUpdateDto,project);
            projectMapper.update(project);
        }

    }


    //根据项目id查询项目
    @Override
    public ProjectUpdateVO selectById(Long id) {
        ProjectUpdateVO projectUpdateVo = projectMapper.selectById(id);
        return projectUpdateVo;
    }

    //查询所有企业和项目
    @Override
    public List<AllProjectVO> selectAll() {


        List<AllProjectVO> allProjectVOList = new ArrayList<>();
        List<String> groupIdList = projectMapper.selectAllGroup();
        groupIdList.forEach(groupId ->{
            AllProjectVO allProjectVO = new AllProjectVO();
            allProjectVO.setGroupId(groupId);
            List<Project> projectList = projectMapper.selectByGroupId(groupId);

            List<ProjectNameID> projectNameIDList = new ArrayList<>();

            projectList.forEach(project ->{
                ProjectNameID projectNameID = new ProjectNameID();
                projectNameID.setProjectName(project.getProjectName());
                projectNameID.setProjectId(project.getId());
                projectNameIDList.add(projectNameID);

            });
            allProjectVO.setProjectList(projectNameIDList);
            allProjectVOList.add(allProjectVO);
        });

        return allProjectVOList;
    }



}
